package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.bean.BeifenBean;
import com.util.DBSource;

public class BeifenDao {
	
	DBSource db=new DBSource();
	
	public int addBeifen(BeifenBean bean) {
		int rows = 0;
		Connection conn = null;
		try{
			conn = db.getConn();
			String sql = "insert into journal(bname,burl,username,act) values(?,?,?,?)";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, bean.getBname());
			ps.setString(2, bean.getBurl());
			
			ps.setString(3, bean.getUsername());
			ps.setString(4, bean.getAct());
			
			rows = ps.executeUpdate();
			ps.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			db.closeConn(conn);
		}
		return rows;
	}
	
	public List<BeifenBean> selectAll() {
		Connection conn = null;
		List<BeifenBean> beifens = new ArrayList<BeifenBean>();
		try{
			conn = db.getConn();
			String sql = "select * from journal ";
			PreparedStatement ps = conn.prepareStatement(sql);
			
	
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				BeifenBean bean = new BeifenBean();
				bean.setId(rs.getInt("id"));
				bean.setBname(rs.getString("bname"));
				bean.setBurl(rs.getString("burl"));
				bean.setUsername(rs.getString("username"));
				bean.setAct(rs.getString("act"));
				
				
				beifens.add(bean);
			}
			rs.close();
			ps.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			db.closeConn(conn);
		}
		
		return beifens;
	}
	
	public String selectByBname(String bname)
	{
		Connection conn = null;
		String burl = "";
		try{
			conn = db.getConn();
			String sql = "select * from journal where bname = ?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, bname);
			
	
			ResultSet rs = ps.executeQuery();
			if(rs.next())
			{
				burl = rs.getString("burl");
			}
			rs.close();
			ps.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			db.closeConn(conn);
		}
		return burl;
	}

}
